Preview crashed because of a Fatal Error in ModelData.

Can anyone give me assistance on how to fix this. My preview crashed because a Fatal Error in ModelData.

This is how my ModelData looks:

import SwiftUI

struct SutraokeDetail: View {

@Environment(ModelData.self) var modelData
var sutra: Sutras


var body: some View {
    @Bindable var modelData = modelData
    
    ScrollView {
        CircleImage(image: sutra.image)
            .offset(y: -130)
            .padding(.bottom, -130)
        VStack(alignment: .leading) {
            HStack{
                Text(sutra.name)
                    .font(.title)
                
                Spacer()
                Text(sutra.text)
                
            }
        }
        
    }
}

} #Preview { let modelData = ModelData() return SutraokeDetail(sutra: modelData.sutras[0]) .environment(modelData) }

Remove: @Bindable var modelData = modelData

There is no correlation between modelData and var sutra, so whatever you're trying to do will not work.

Assuming you are using SwiftData based on the SwiftData tag being present, most crashes involving SwiftData and Previews are because you need to create and set a ModelContainer in the environment of the view that you are previewing. To get a more specific answer, you will probably need to share more details about the error and perhaps the ModelData code that is crashing.

Here is another forum post with an answer describing one way to setup the ModelContainer for previews: https://forums.developer.apple.com/forums/thread/745185

Preview crashed because of a Fatal Error in ModelData.
 
 
Q